var express = require('express');
var jwt = require('jsonwebtoken');
var CONFIG = require('./../config');
var usersDao = require('./../dao/users');
require('./../util/common');
require('./../util/bootloader');
var router = express.Router();

/* GET users listing. */
router.get('/', function (req, res, next) {
    res.send('respond with a resource');
});

//进行token的生成
router.post('/created_token', function (req, res, next) {
    var par = paramAll(req);

    var userInfo = {
        account: par.account,
        password: encryPassword(par.password),
        app_sid: par.app_sid
    }

    var token = jwt.sign(userInfo, CONFIG.TOKENSECRET, {
        expiresIn: '48h',
        issuer: 'niyueling'
    });

    return res.json(new PKG(token));
});

//反向解析token获得用户信息
router.post('/get_user_info', function (req, res, next) {
    var par = paramAll(req);

    jwt.verify(par.token, CONFIG.TOKENSECRET, (err, data) => {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            return res.json(new PKG(data));
        }
    });
});

//用户登陆
router.post('/login', function (req, res, next) {
    var par = paramAll(req);

    //1.加密生成签名sign
    var param = createSign({
        account: par.account,
        password: encryPassword(par.password),
        app_sid: par.app_sid
    }, 'niyueling111222333');

    if (param.sign == param.sign) {
        //2.签名验证成功，数据库验证账号密码是否匹配
        usersDao.login(param, function (err, data) {
            if (err) {
                return res.json(new ERR(err, 410));
            } else {
                if (data.status == 200) {
                    //账号密码匹配生成token与用户信息一起返回给前端
                    data.token = jwt.sign(param, CONFIG.TOKENSECRET, {
                        expiresIn: '48h',
                        issuer: 'niyueling'
                    });
                    return res.json(new PKG(data));
                } else {
                    return res.json(new ERR(data, 420));
                }
            }
        });
    }
});

//查询用户积分信息
router.post('/get_user_score', function (req, res, next) {
    var par = paramAll(req);

    //1.加密生成签名sign
    var param = createSign({
        account: par.account,
        app_sid: par.app_sid,
        token: par.token
    }, 'niyueling111222333');

    if (param.sign == param.sign) {
        //2.签名验证成功，查看token解析账号是否和传参账号一致
        jwt.verify(par.token, CONFIG.TOKENSECRET, (err, data) => {
            if (err) {
                return res.json(new ERR(err, 410));
            } else {
                if (par.account == data.account) {
                    //2.token解析数据成功并且账号一致，则查询用户当前积分数据并且返回
                    usersDao.get_user_score(param, function (err, data) {
                        if (err) {
                            return res.json(new ERR(err, 410));
                        } else {
                            if (data.status == 200) {
                                return res.json(new PKG(data));
                            } else {
                                return res.json(new ERR(data, 420));
                            }
                        }
                    });
                } else {
                    return res.json(new ERR('token解析用户数据不匹配!', 420));
                }
            }
        });
    }
});

module.exports = router;